<?php

namespace app\admin\controller;

use think\Request;
use app\admin\model\UserLevel as UserLevelModel;
use think\Paginator;

Class UserLevel extends Base
{

    //等级页面
    public function level()
    {
        //常量
        $webSite = WEB_SITE;
        $staticAdmin = STATIC_ADMIN;

        //遍历等级表
        $level = UserLevelModel::all();

        $flag = 'level';    //标记位
        $lid1 = '';
        if (Request::instance()->param('lid1')) {
            $lid1 = Request::instance()->param('lid1');
            $flag = 'lid';
        }
        //dump($flag);
        //传值,渲染
        $this->assign([
            'webSite'     => $webSite,
            'staticAdmin' => $staticAdmin,
            'level'   => $level,
            'flag'    => $flag,
            'lid1'    => $lid1,
        ]);
        return $this->fetch();

    }

    //添加
    public function add()
    {
        //取值
        $post = Request::instance()->param();
        $name = $post['levName'];
        $grade = $post['levGrade'];
        $discount = $post['levCount'];

        //当前时间datetime格式
        $currTime = date('Y-m-d H:i:s',time());
        //dump($currTime);

        $level = UserLevelModel::create(['level_name'=>"$name",'level_grade'=>$grade,'discount'=>$discount,'create_time'=>"$currTime,'update_time'=>$currTime"]);

        //是否添加成功
        if ($level) {

            echo json_encode(['state'=>1,'tip'=>"添加成功"]);
        } else {
            echo json_encode(['state'=>2,'tip'=>'添加失败']);
        }


    }

    //编辑
    public function update()
    {
        $lid1 = Request::instance()->param('lid1');
        $str = Request::instance()->param('str');
        //dump($str);

        //取出键值对
       $str = trim($str,'"');
        $arr[] = explode(',',$str);

        //还原关联数组
        //当前时间datetime格式
        $currTime = date('Y-m-d H:i:s',time());
        $data['update_time'] = $currTime;

        foreach ($arr as  $value) {
            //dump($value);
            foreach ($value as $val) {
                $rows = explode('===',$val);
                $data[$rows[0]] = $rows[1];
                //dump($rows);
            }
        }
        //dump($rows);
        //dump($data);

        //修改数据
        $level = UserLevelModel::where(['level_id'=>$lid1])->update($data);

         //是否修改成功
         if ($level) {
             echo json_encode(['status'=>1,'tip'=>'修改成功']);
         } else {
             echo json_encode(['status'=>2,'tip'=>'修改失败']);
         }
    }


    //删除 (a标签url跳转)
    public function delete()
    {
        $lid2 = Request::instance()->param('lid2');

        //删除数据
        $level = UserLevelModel::where(['level_id'=>$lid2])->delete();

        //是否删除成功
        if ($level) {

            //$this->success('删除成功','level');
            echo json_encode(['state2'=>1,'tip2'=>'删除成功']);
        } else {

            //$this->error('删除失败','level');
            //查询当前项是否存在
            $res = UserLevelModel::where(['level_id'=>$lid2])->select();
            if (!$res) {
                echo json_encode(['state2'=>3,'tip2'=>'当前选项不存在']);
            } else {
                echo json_encode(['state2'=>2,'tip2'=>'删除失败']);
            }
        }
    }

}